<template>
	<view class="config">
		<custom-head-top>
			<template #left>
				项目配置
			</template>
		
			<template #right>
				<button type="primary" size="mini" :plain="!btnType" @click="editAndSubmit">
					<template v-if="btnType">
						<uni-icons type="paperplane-filled" size="14" color="#fff"></uni-icons>
						提交
					</template>
					<template v-else>
						<uni-icons type="compose" size="14" color="#007AFF"></uni-icons>
						编辑
					</template>
				</button>
			</template>
			<view class="main">
				<view class="container">
					
				</view>
			</view>
		</custom-head-top>
		
		<view class="main" :class="!btnType ? 'readonly' : ''">
			<view class="container">
				<uni-forms ref="formRef" :modelValue="formData" label-align="right" :label-width="120">	
					<uni-forms-item label="LOGO" name="logo">
						<select-one-img :width="100" v-model:formData="formData" ratio="1 / 1" :maxImgSize="500" 
							@update:formData="(e)=>{if(e.picurl==''){formData.logo=''}}"></select-one-img>
					</uni-forms-item>
					
					<uni-forms-item label="项目名称" name="brand">
						<uni-easyinput type="text" v-model="formData.brand" placeholder="请输入项目名称"></uni-easyinput>
					</uni-forms-item>
					
					<uni-forms-item label="是否开启广告" name="checkedAd">
						<switch :checked="formData.checkedAd" class="switchStyle" @change="checkedAdChange"/>
					</uni-forms-item>
					
					<uni-forms-item label="激励视频ID" name="rewardedVideo">
						<uni-easyinput type="text" v-model="formData.rewardedVideo" placeholder="请输入激励视频ID"></uni-easyinput>
					</uni-forms-item>
					
					<uni-forms-item label="视频卡片ID" name="cardVideo">
						<uni-easyinput type="text" v-model="formData.cardVideo" placeholder="请输入视频卡片ID"></uni-easyinput>
					</uni-forms-item>
					
					<uni-forms-item label="每日领币数" name="dayCoin" v-if="formData.checkedAd">
						<uni-easyinput type="text" v-model.number="formData.dayCoin" placeholder="请输入每日领币数"></uni-easyinput>
					</uni-forms-item>
					
					<uni-forms-item label="看广告得币" name="adCoin" v-if="formData.checkedAd">
						<uni-easyinput type="text" v-model.number="formData.adCoin" placeholder="请输入看广告得币数"></uni-easyinput>
					</uni-forms-item>
					
					<uni-forms-item label="硬币规则" name="ruleCoin" v-if="formData.checkedAd">
						<uni-easyinput type="textarea" v-model="formData.ruleCoin" placeholder="请输入硬币规则"  maxlength="300"></uni-easyinput>
					</uni-forms-item>
					
					<uni-forms-item label="搜索热词" name="hots">
						<uni-easyinput type="text" v-model="formData.hots" placeholder="请输入搜索热词用逗号,隔开" />
					</uni-forms-item>
					
					<uni-forms-item label="版权信息" name="copyright">
						<uni-easyinput type="textarea" v-model="formData.copyright"
						placeholder="请输入版权信息" maxlength="300"/>
					</uni-forms-item>
					
					<uni-forms-item label="服务协议" name="service">
						<uni-easyinput type="textarea" v-model="formData.service" placeholder="请输入服务协议" autoHeight maxlength="1000"/>
					</uni-forms-item>
					
					<uni-forms-item label="隐私政策" name="privacy">
						<uni-easyinput type="textarea" v-model="formData.privacy" placeholder="请输入隐私政策" autoHeight maxlength="1000"/>
					</uni-forms-item>
				</uni-forms>
			</view>
		</view>
	</view>
</template>


<script setup>
import { computed, ref } from 'vue';
import { showModal, showToast, uploadFileItem } from '../../../utils/common';
import { cloudToHttps } from '../../../utils/tools';

const configCloudObj = uniCloud.importObject("admin-system");
const formRef = ref(null);
const btnType = ref(0); //0是编辑  1是提交
const btnName = computed(()=>btnType.value?'提交':'编辑');
const formData = ref({
	logo:"",
	brand:"",
	checkedAd:true,
	rewardedVideo:"",//激励视频id
	cardVideo:"",//视频卡片广告
	dayCoin:10, //每日领币
	adCoin:30,  //看广告获得币
	ruleCoin:"", //硬币规则说明
	hots:"",//搜索热词
	copyright:"",
	service:"",
	privacy:""
});


const checkedAdChange = (e) => {
	formData.value.checkedAd = e.detail.value;
}

//提交编辑修改
const editAndSubmit = async() => {
	if(btnType.value) {
		try {
			uni.showLoading({mask:true})
			if(formData.value.tmpurl && formData.value.tmpurl != formData.value.logo) {
				let file = await uploadFileItem(formData.value.tmpurl)
				formData.value.logo = await cloudToHttps(file.fileID);
			}
			let {_id, tmpurl, picurl, ...params} = formData.value;
			let {errCode} = await configCloudObj.updateConfig(params);
			if(errCode != 0) return showToast({title: "更新失败"});
			showToast({title:"更新成功"});
		} catch (err) {
			console.log(err);
			uni.hideLoading();
			showToast({title:err})
		}
	} else {
		console.log("编辑");
	}
	btnType.value = !btnType.value ? 1 : 0;
}

//获取数据
const getData = async() => {
	let {errCode, data} = await configCloudObj.getConfig();
	if(errCode != errCode) showToast({title: "数据查询失败"});
	formData.value = {...formData.value, ...data, tmpurl: data.logo};
}

getData();
	
</script>


<style lang="scss" scoped>
	.main {
		padding: 20px;
		.container{
			border: 1px solid red;
			min-height: 200px;
			max-width: 1000px;
		}
		:deep(.uni-forms-item){
			margin-top: 32px;
		}
		:deep(.uni-forms-item__content){
			display: flex;
			align-items: center;
		}
	}
	
	.readonly {
		pointer-events: none;
		opacity: 0.7;
		user-select: none;
	}
</style>